﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace eventModel
{
    /// <summary>Used to provide a UI to monitor the server.</summary>
    public partial class ServerControl : Form
    {
        public ServerControl()
        {
            // Sets up controls & updates them:
            InitializeComponent();
            RefreshData();
        }

        /// <summary>Updates controls</summary>
        public void RefreshData()
        {
            //Most of this code straight up reads variables that the main or worker threads may be in the process of editing...
            //race conditions could happen very easily, but I think this is okay because I don't care if the data is a bit old 
            // and these are R/O calls, but I'm open to suggestions... (mostly I just wanted to be able to show SOMETHING on screen)

            lblHostBindings.Text = "Host Bindings: ";
            ServerEntry.HostBindings.ForEach(b => lblHostBindings.Text += b);

            lblRequestsServed.Text = "Requests Served: " + Pipeline.RequestsServed.ToString();
            lblAverageResponseTime.Text = "Average Response Time: (in millisecs) " + (Pipeline.TimeTaken / Pipeline.RequestsServed).ToString();

            txtBxServingDir.Text = Pipeline.ServerDir;
            txtBxExecutableExtension.Text = "";
            ServerEntry.ExecutableExtensions.ForEach(e => txtBxExecutableExtension.Text += e);

            chkBxAllowExecutables.Checked = Pipeline.AllowExecutables;
            chkBxAllowLogging.Checked = Pipeline.EnableLogging;

            Logging.Entries.OrderByDescending(e => e.TimeStamp).ToList().ForEach(e => dtGrdVwLogs.Rows.Add(new object[] { e.TimeStamp, e.Message }));
        }

        private void btnClose_Click(object sender, EventArgs e) { this.Close(); }

        private void btnRefresh_Click(object sender, EventArgs e) { this.RefreshData(); }

        private void button3_Click(object sender, EventArgs e) { ServerInterface.Exit_Click(null,null); }
    }
}
